﻿#region
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevTools.Common.Components;
using DevTools.Properties;

#endregion

namespace DevTools.EntityPackGenerator
{
    public partial class EntityPackGeneratorForm : RootForm
    {
        public EntityPackGeneratorForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (DesignMode)
                return;

            txeFolder.Text = Settings.Default.EntityPackGenerator_FolderToGenerate;
            txeNamespace.Text = Settings.Default.EntityPackGenerator_Namespace;

            mmeForm_cs.Text = Settings.Default.EntityPackGenerator_Form_cs;
            mmeForm_Designer_cs.Text = Settings.Default.EntityPackGenerator_Form_Designer_cs;
            mmeForm_resx.Text = Settings.Default.EntityPackGenerator_Form_resx;
            mmeSearchControl_cs.Text = Settings.Default.EntityPackGenerator_SearchControl_cs;
            mmeSearchControl_Designer_cs.Text = Settings.Default.EntityPackGenerator_SearchControl_Designer_cs;
            mmeSearchControl_resx.Text = Settings.Default.EntityPackGenerator_SearchControl_resx;
            mmeListControl_cs.Text = Settings.Default.EntityPackGenerator_ListControl_cs;
            mmeListControl_Designer_cs.Text = Settings.Default.EntityPackGenerator_ListControl_Designer_cs;
            mmeListControl_resx.Text = Settings.Default.EntityPackGenerator_ListControl_resx;
            mmeListForm_cs.Text = Settings.Default.EntityPackGenerator_ListForm_cs;
            mmeListForm_Designer_cs.Text = Settings.Default.EntityPackGenerator_ListForm_Designer_cs;
            mmeListForm_resx.Text = Settings.Default.EntityPackGenerator_ListForm_resx;

            if (string.IsNullOrEmpty(Settings.Default.EntityPackGenerator_Form_cs))
                LoadInitialData();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            Settings.Default.EntityPackGenerator_FolderToGenerate = txeFolder.Text;
            Settings.Default.EntityPackGenerator_Namespace = txeNamespace.Text;

            Settings.Default.EntityPackGenerator_Form_cs = mmeForm_cs.Text;
            Settings.Default.EntityPackGenerator_Form_Designer_cs = mmeForm_Designer_cs.Text;
            Settings.Default.EntityPackGenerator_Form_resx = mmeForm_resx.Text;
            Settings.Default.EntityPackGenerator_SearchControl_cs = mmeSearchControl_cs.Text;
            Settings.Default.EntityPackGenerator_SearchControl_Designer_cs = mmeSearchControl_Designer_cs.Text;
            Settings.Default.EntityPackGenerator_SearchControl_resx = mmeSearchControl_resx.Text;
            Settings.Default.EntityPackGenerator_ListControl_cs = mmeListControl_cs.Text;
            Settings.Default.EntityPackGenerator_ListControl_Designer_cs = mmeListControl_Designer_cs.Text;
            Settings.Default.EntityPackGenerator_ListControl_resx = mmeListControl_resx.Text;
            Settings.Default.EntityPackGenerator_ListForm_cs = mmeListForm_cs.Text;
            Settings.Default.EntityPackGenerator_ListForm_Designer_cs = mmeListForm_Designer_cs.Text;
            Settings.Default.EntityPackGenerator_ListForm_resx = mmeListForm_resx.Text;

            Settings.Default.Save();

            base.OnClosing(e);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (fbdFolder.ShowDialog() != DialogResult.OK)
                return;

            txeFolder.Text = fbdFolder.SelectedPath;
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            var memoEdits = WinformUtility.GetChildren<MemoEdit>(this);

            foreach (var memoEdit in memoEdits)
            {
                var name = memoEdit.Name.Substring(3).Replace("_", ".");
                var content = memoEdit.Text.Replace("%Entity%", txeEntity.Text).Replace("%Namespace%", txeNamespace.Text);
                File.WriteAllText(txeFolder.Text + "\\" + txeEntity.Text + name, content);
            }

            MessageBox.Show("Done");
        }

        private void LoadInitialData()
        {
            var memoEdits = WinformUtility.GetChildren<MemoEdit>(this);

            foreach (var memoEdit in memoEdits)
            {
                var name = memoEdit.Name.Substring(3).Replace("_", ".");
                var content = File.ReadAllText("EntityPackGenerator\\Templates\\" + name);
                memoEdit.Text = content;
            }
        }
    }
}